package com.azkj.album.view.widgets;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.azkj.album.MyApplication;
import com.azkj.album.utils.Utils;

@SuppressLint("AppCompatCustomView")
public class ScrollTextView extends TextView {

    public ScrollTextView(Context context) {
        super(context);
    }

    public ScrollTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public ScrollTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public ScrollTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    public void setText(String text) {

        if (TextUtils.isEmpty(text) || Utils.GetByteLength(text) < 12) {
            setGravity(Gravity.CENTER);
            setMovementMethod(null);//设置可横向滚动
            setClickable(true);
            setLongClickable(true);
            setEnabled(true);
            setHorizontallyScrolling(false);
        } else {
            setGravity(Gravity.CENTER_VERTICAL);
            setMovementMethod(ScrollingMovementMethod.getInstance());//设置可横向滚动
            setHorizontallyScrolling(true);
        }
        MyApplication.getCurrentActivity().runOnUiThread(() -> super.setText(text));
    }
}
